package com.example.controller.backend;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.example.common.ApiResult;
import com.example.entity.User;
import com.example.service.UserService;
import com.example.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/api/manager/admin")
@Api(tags = "用户管理")
public class BUserController {
    @Resource
    private UserService userService;

    @ApiOperation("分页查询用户列表")
    @GetMapping("/findPage")
    public ApiResult findPage(@RequestParam(required = false,defaultValue = "") String nickName,
                              @RequestParam(defaultValue = "1")Integer page,
                              @RequestParam(defaultValue = "10") Integer size){
        Page<User> itemPage = new Page<>(page, size);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like("nick_name",nickName);
        queryWrapper.orderByDesc("create_date");
        Page<User> result = userService.page(itemPage,queryWrapper);
        PageDTO<User> pageDTO = new PageDTO<>();
        pageDTO.setTotal(result.getTotal());
        pageDTO.setRecords(result.getRecords());
        return ApiResult.ok(pageDTO);
    }

    @ApiOperation("新增用户")
    @PutMapping("/addUser")
    public ApiResult addUser(@RequestBody User user){
        boolean b = userService.save(user);
        if (b==true){
            return ApiResult.ok(b);
        }
        return ApiResult.fail("新增失败");
    }

    @ApiOperation("更新用户")
    @PutMapping("/updateUser")
    public ApiResult updateUser(@RequestBody User user){
        boolean b = userService.updateById(user);
        if (b==true){
            return ApiResult.ok(b);
        }
        return ApiResult.fail("更新失败");
    }

    @ApiOperation("删除用户")
    @DeleteMapping("/deleteUser")
    public ApiResult deleteUser(@RequestBody Integer id){
        boolean b = userService.removeById(id);
        if (b==true){
            return ApiResult.ok(b);
        }
        return ApiResult.fail("删除失败");
    }

    @ApiOperation("停用用户")
    @PostMapping("/deactivateUser")
    public ApiResult deactivateUser(@RequestParam Integer id, @RequestParam(defaultValue = "0") Integer userStatus){
        UpdateWrapper updateWrapper=new UpdateWrapper();
        updateWrapper.eq("id",id);
        updateWrapper.set("user_status",userStatus);
        boolean b = userService.update(updateWrapper);
        if (b==true){
            return ApiResult.ok(b);
        }
        return ApiResult.fail();
    }

    @ApiOperation("管理员登录")
    @PostMapping("/login")
    public ApiResult login(@RequestBody UserVO userVO){
        return userService.adminLogin(userVO);
    }
}
